@echo off
SETLOCAL EnableDelayedExpansion

:: 定义日志文件的路径
SET "changeLogPath=logs\git-status-result-change.log"
SET "cleanLogPath=logs\git-status-result-clean.log"
SET "errorLogPath=logs\git-status-result-error.log"

:: 创建logs文件夹如果它不存在
IF NOT EXIST "logs" mkdir "logs"

:: my-repos-path.txt 是存储git仓库路径的文件，每个路径一行。
SET "filePath=D:\code_zch\code_go\GolandProjects\get-git-repos-status\my-repos-path.txt"

:: 确保日志文件存在
IF NOT EXIST "%changeLogPath%" echo. > "%changeLogPath%"
IF NOT EXIST "%cleanLogPath%" echo. > "%cleanLogPath%"
IF NOT EXIST "%errorLogPath%" echo. > "%errorLogPath%"

:: 读取文件并检查每个仓库
FOR /F "tokens=*" %%i IN (%filePath%) DO (
    SET "repoPath=%%i"
    SET "repoPath=!repoPath:~0,-1!" :: 移除行尾的换行符
    PUSHD !repoPath!
    git status > nul 2>&1
    IF !ERRORLEVEL! NEQ 0 (
        echo Repository: !repoPath! Error: Unable to check status.>>"!errorLogPath!"
    ) ELSE (
        FOR /F %%x IN ('git status') DO SET status=%%x

        IF "!status!" == "" (
            echo Repository: !repoPath! Status: No changes.>>"!cleanLogPath!"
        ) ELSE (
            echo Repository: !repoPath! Status: Changes detected.>>"!changeLogPath!"
        )
    )
    POPD
)

echo Completed checking repositories.
ENDLOCAL